/**
 * Created by JQ on 2017/1/16.
 */
module.exports = function ($http,httpservice,$q) {
    var calendarService = {};


    /**
     * 新增日程任务
     * @param result1
     * @param taskcolour
     * @param timenow
     * @param timefinish
     * @param loginId
     * @param userIds
     * @param username
     */
    calendarService.establishtask = function (result1, taskcolour, timenow, timefinish, loginId, userIds, username, searchType, caseId, taskId, teamId,remindPoint,result2) {
        var params = {
            "schedule": result1,
            "colour": taskcolour,
            "beginTime": timenow,
            "endTime": timefinish,
            "creatorId": loginId,
            "userIds": loginId + "!%!" + userIds,
            "username": username,
            "searchType": searchType,
            "caseid": caseId,
            "taskId": taskId,
            "teamId": teamId,
            "remindPoint":remindPoint,
            "description":result2
        };


        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.saveGlobalCalendarTaskUrl,
            data: params,
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 全局日程
     * @param id
     * @returns {Function}
     */
    calendarService.gaintaklist= function (id) {
        var params={"teamId":id};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.gainCalendarTaskUrl,
            data:params,
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 案件日程任务
     * @param id
     * @param selectedUsers
     * @returns {Function}
     */
    calendarService.casetasklist= function (id, selectedUsers,caseId) {
      var params={"bigLawuserid":id, "selectedUsers":selectedUsers, "caseid":caseId};
      var deferred = $q.defer();
      $http({
        method: 'POST',
        url: httpservice.caseCalendarTaskUrl,
        data:params,
        //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        headers: {'Content-Type': 'application/json'}
      }).then(function successCallback(response) {
        deferred.resolve(response.data);
      }, function errorCallback(response) {
        deferred.reject(response);
      });
      return deferred.promise;
    };

    /**
     * 删除日程任务
     * @param id 日程ID
     * @param username 登录姓名
     * @param userId 登录ID
     */
    calendarService.deleteCalendar = function (caseId,id,username,userId) {
        var params = {"caseid":caseId ,"id": id,"username":username,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteCalendarTaskUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 更新日程任务
     * @param id 任务ID
     * @param schedule 标题
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @param colour 颜色
     * @param userId 登录ID
     */
    calendarService.updateCalendar = function (caseId, id, schedule, beginTime, endTime, colour, userId,remindPoint,description) {
        var params = {
            "caseid":caseId,
            "id": id,
            "schedule": schedule,
            "beginTime": beginTime,
            "endTime": endTime,
            "colour": colour,
            "userId": userId,
          "remindPoint":remindPoint,
          "description":description
        };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateCalendarTaskUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 查询律师成员
     * @param email
     * @param cellphone
     * @constructor
     */
    calendarService.CaseLawyerTian = function (keyword, searchType, caseId, taskId, teamId) {
        //此处修改了 搜索的参数
        var params={"keyword":keyword,"searchType":searchType, "caseid":caseId, "taskId":taskId, "teamId":teamId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.caluserUrl,
            params:params,
            //headers : {'Content-Type':'application/json'}
            //  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 添加和删除参与人
     * @param workScheduleId 日程ID
     * @param userId 参与人ID
     * @param status 1- 已接受；3- 已发邀请；5-已拒绝；7-已退出；9-已删除
     * @param loginName 登录名称
     * @param loginId 登录ID
     */
    calendarService.updateWorksCheduleParticipant = function (workScheduleId, userId, status, loginName, loginId) {
        var params = {
            "workScheduleId": workScheduleId,
            "userId": userId,
            "status": status,
            "loginName": loginName,
            "loginId": loginId
        };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateWorksCheduleParticipant,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /**
     * 获取参与人列表
     * @param workScheduleId 日程ID
     */
    calendarService.queryWorksCheduleParticipant = function (workScheduleId) {
        var params = {"workScheduleId": workScheduleId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.queryWorksCheduleParticipant,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };




    return calendarService;
};
